﻿// 示例代码4-1
// 异常的规范
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// vs2019 debug x86
// warning “Divide” : 假定函数不引发异常，但确实发生了
double Divide(double a, double b) noexcept
{
	if (b == 0)
	{
		throw "Division by zero condition!";
	}
	return a / b;
}

void Test5()
{
	try
	{
		double a, b;
		cin >> a >> b;
		cout << Divide(a, b) << endl;
	}
	catch (const char* errmsg)
	{
		cout << errmsg << endl;
	}
	catch (...)
	{
		cout << "Unkown Exception" << endl;
	}

	int i = 0;
	cout << noexcept(Divide(1, 2)) << endl;
	cout << noexcept(Divide(1, 0)) << endl;
	cout << noexcept(++i) << endl << endl;

	string s = "123";
	cout << noexcept(s.begin()) << endl;
	cout << noexcept(string("123").begin()) << endl;
	cout << noexcept(string().begin()) << endl << endl;

	vector<int> v;
	cout << noexcept(v.begin()) << endl;
	cout << noexcept(vector<int>({ 1, 2, 3 }).begin()) << endl;
	cout << noexcept(vector<int>().begin()) << endl;
}

int main()
{
	Test5();

	return 0;
}

